package cn.highedu.coolsharkhub.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*Cookies演示控制器*/
@RestController
public class CookiesController {
    private Logger logger = LoggerFactory.getLogger(CookiesController.class);

    /*下发Cookies*/
    @GetMapping("/setcookies")
    public String setCookies(HttpServletResponse response) {
        logger.info("下发Cookies");
        //创建Cookie
        Cookie cookie = new Cookie("username", "HighEDU");
        //设置Cookie的有效路径
        cookie.setPath("/");
        //设置Cookie的有效时间 单位：秒
        cookie.setMaxAge(60 * 60 * 24 * 7);
        //下发Cookie
        response.addCookie(cookie);
        return "下发Cookies";
    }

    /* 获取Cookies*/
    @GetMapping("/getcookies")
    public String getCookies(@CookieValue String username) {
        logger.info("获取Cookies");
        logger.info("Cookies: {}", username);
        return "获取Cookies";
    }
}
